package com.geeyuu.android.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.TextUtils;
import android.util.Log;

import com.geeyuu.android.GeeConfig;

public class LogUtil {
    /**
     * 普通信息
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void v(String tag, String msg, Throwable e) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg) && e == null) {
                Log.v(tag, msg);
            } else if (!TextUtils.isEmpty(msg) && e != null) {
                Log.v(tag, msg, e);
            }
        }
    }

    /**
     * 调试信息
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void d(String tag, String msg, Throwable e) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg) && e == null) {
                Log.d(tag, msg);
            } else if (!TextUtils.isEmpty(msg) && e != null) {
                Log.d(tag, msg, e);
            }
        }
    }

    /**
     * 重要信息
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void i(String tag, String msg, Throwable e) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg) && e == null) {
                Log.i(tag, msg);
            } else if (!TextUtils.isEmpty(msg) && e != null) {
                Log.i(tag, msg, e);
            }
        }
    }

    /**
     * 意料之中的较严重异常
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void w(String tag, String msg, Throwable e) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg) && e == null) {
                Log.w(tag, msg);
            } else if (!TextUtils.isEmpty(msg) && e != null) {
                Log.w(tag, msg, e);
            }
        }
    }

    /**
     * 意料之外的严重异常
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void e(String tag, String msg, Throwable e) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg) && e == null) {
                Log.e(tag, msg);
            } else if (!TextUtils.isEmpty(msg) && e != null) {
                Log.e(tag, msg, e);
            }
        }
    }

    /**
     * 普通信息
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void v(String tag, String msg) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg)) {
                Log.v(tag, msg);
            }
        }
    }

    /**
     * 调试信息
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void d(String tag, String msg) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg)) {
                Log.d(tag, msg);
            }
        }
    }

    /**
     * 重要信息
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void i(String tag, String msg) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg)) {
                Log.i(tag, msg);
            }
        }
    }

    /**
     * 意料之中的较严重事件
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void w(String tag, String msg) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg)) {
                Log.w(tag, msg);
            }
        }
    }

    /**
     * 意料之外的严重事件
     * 
     * @param tag
     * @param msg
     * @param e
     */
    public static void e(String tag, String msg) {
        if (GeeConfig.isPrintLog && !TextUtils.isEmpty(tag)) {
            if (!TextUtils.isEmpty(msg)) {
                Log.e(tag, msg);
            }
        }
    }

    // lyc
    private static final String sOutputFileName = "/data/data/com." + "GeeYuu" + "/Log.txt";

    public static void sign() {
        signPrivate(4);
    }

    public static void sign(String strSignContent) {
        signPrivate(4);

        if (strSignContent == null) {
            Log.i("SignData", "null");
        } else {
            Log.i("SignData", strSignContent);
        }
    }

    public static void sign(int nSignContent) {
        signPrivate(4);
        Log.i("SignData", "" + nSignContent);
    }

    public static void sign(float fSignContent) {
        signPrivate(4);
        Log.i("SignData", "" + fSignContent);
    }

    public static void sign(long lSignContent) {
        signPrivate(4);
        Log.i("SignData", "" + lSignContent);
    }

    public static void sign(byte byteSignContent) {
        signPrivate(4);
        Log.i("SignData", "" + byteSignContent);
    }

    public static void sign(boolean bSignContent) {
        signPrivate(4);
        Log.i("SignData", "" + bSignContent);
    }

    public static void signGrade(int nGradeCount) {
        for (int i = 0; i < nGradeCount; i++) {
            signPrivate(4 + i);
        }
    }

    private static void signPrivate(int nGrade) {
        Log.i("SignData", "File:" + Thread.currentThread().getStackTrace()[nGrade].getFileName()
                + " Line:" + Thread.currentThread().getStackTrace()[nGrade].getLineNumber()
                + " Class:" + Thread.currentThread().getStackTrace()[nGrade].getClassName()
                + " Method:" + Thread.currentThread().getStackTrace()[nGrade].getMethodName());
    }

    public static void error() {
        addLog(null);
    }

    public static void log(String strContent) {
        addLog(strContent);
    }
    
    public static void log(byte[] btLogContent) {
        String strLogContent = "";

        try {
            strLogContent = new String(btLogContent, "GB2312");
        } catch (UnsupportedEncodingException e) {
        }

        addLog(strLogContent);
    }
    
    private static void addLog(String strLogContent) {
        final int nGrade = 4;
        final String strTab = "[]";
        final String strEndline = "[!-]";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String strTitle = simpleDateFormat.format(new Date()) + strTab + "File:"
                + Thread.currentThread().getStackTrace()[nGrade].getFileName() + strEndline
                + strTab + strTab + strTab + "Line:"
                + Thread.currentThread().getStackTrace()[nGrade].getLineNumber() + strEndline
                + strTab + strTab + strTab + "Class:"
                + Thread.currentThread().getStackTrace()[nGrade].getClassName() + strEndline
                + strTab + strTab + strTab + "Method:"
                + Thread.currentThread().getStackTrace()[nGrade].getMethodName() + strEndline;

        try {
            FileWriter file = new FileWriter(sOutputFileName, true);

            file.append(strTitle);

            if (strLogContent == null) {
                file.append("\r\n");
            } else {
                file.append(strTab + strTab + strTab + strLogContent + "\r\n");
            }

            file.close();
        } catch (IOException e) {
        }
    }
    
    public static String getLog() {
        String strResult = "";

        try {
            FileReader fileReader = new FileReader(sOutputFileName);

            BufferedReader bufferReader = new BufferedReader(fileReader, 500);
            String strLine;

            while ((strLine = bufferReader.readLine()) != null) {
                strResult += strLine;
                strResult += "\r\n";
            }

            bufferReader.close();
            fileReader.close();
        } catch (FileNotFoundException e) {
            LogUtil.log(e.toString());
        } catch (IOException e) {
            LogUtil.log(e.toString());
        }

        return strResult;
    }

    public static void clear() {
        File file = new File(sOutputFileName);

        if (file == null) {
            LogUtil.error();
            return;
        }

        if (!file.delete()) {
            LogUtil.error();
        }
    }
}
